define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'baoan/msg/index' + location.search,
                    add_url: 'baoan/msg/add',
                    // edit_url: 'baoan/msg/edit',
                    // del_url: 'baoan/msg/del',
                    multi_url: 'baoan/msg/multi',
                    import_url: 'baoan/msg/import',
                    table: 'kf_record_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'rid', title: __('Rid')},
                        {field: 'record.car_num', title: __('Record.car_num'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'record.phone', title: __('Record.phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('Type'),searchList: {
                                1: __('报案'),
                                2: __('咨询'),
                                3: __('退费'),
                                4: __('销案退费'),
                                5: __('客服催办'),
                                6: __('投诉'),
                                7: __('工商投诉'),
                                8: __('公众号投诉'),
                                9: __('支付投诉'),
                                10: __('渠道催办'),
                            },
                            custom: {
                                1: 'primary',
                                2: 'default',
                                3: 'warning',
                                4: 'info',
                                5: 'huang',
                                6: 'success',
                                7: 'danger',
                                8: 'primary',
                                9: 'default',
                                10: 'danger',
                            },formatter: Table.api.formatter.label},
                        {field: 'content', title: __('Content'), operate: 'LIKE', table: table, class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }},

                        {field: 'cuiban_beizhu', title: __('催办备注'), operate: false, table: table, class: 'autocontent',formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else  {
                                    return '-';
                                }
                            }},
                        {field: 'date', title: __('时间'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'user_name', title: __('客服名称'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }},
                        // {field: 'fenpei', title: __('Fenpei')},
                        // {field: 'baoan_id', title: __('Baoan_id')},
                        {field: 'is_read', title: __('Is_read'),
                            searchList: {
                                0: __('未读'),
                                1: __('已读'),
                            },
                            custom: {
                                0: 'danger',
                                1: 'primary',
                            },formatter: Table.api.formatter.label
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                // {
                                //     name: 'click',
                                //     title: __('点击执行事件'),
                                //     text: __('标记已读'), // 按钮文字，语言在对应的lang文件设置
                                //     classname: 'btn btn-xs btn-info btn-click',
                                //     icon: '',
                                //     visible: function (row) {
                                //         //权限判断
                                //         if (row.is_read == 0) {
                                //             //返回true时按钮显示,返回false隐藏
                                //             return true;
                                //         }
                                //     },
                                //     // dropdown: '更多',//如果包含dropdown，将会以下拉列表的形式展示
                                //     click: function (data,row) {
                                //         layer.confirm('确认标记已读吗？', {
                                //             btn: ['确定', '取消'], //按钮
                                //             shade: false //不显示遮罩
                                //         }, function(index) {
                                //             console.log('row',row)
                                //             $.ajax({
                                //                 url: 'baoan/msg/yidu/ids/'+row.id, // 你的后端接口URL
                                //                 type: 'get', // 或 'PUT'，取决于你的API设计
                                //                 success: function (ret) {
                                //                     layer.msg('标记成功')
                                //                     layer.close(index);
                                //                     $("#table").bootstrapTable('refresh', {});
                                //                 }
                                //             })
                                //         })
                                //     }
                                // },
                                {
                                    name: 'add',
                                    text: __('录入备注'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('录入备注'), // 提示
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: '', // 更换图标
                                    url: 'baoan/beizhu/adds/ids/{rid}',
                                    extend: 'data-area=["100%","100%"]'
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'baoan/msg/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'baoan/msg/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'baoan/msg/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
